package org.example.ychdp.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.ychdp.dao.ConcertDao;
import org.example.ychdp.dao.OrderDao;
import org.example.ychdp.dao.SeatDao;
import org.example.ychdp.entity.*;
import org.example.ychdp.dao.TicketDao;
import org.example.ychdp.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SeatDao seatDao;
    public Order insertOrder(Order order) {
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        Date currentDate = calendar.getTime();
        order.setOrdertime(currentDate);

        // 提取年、月、日、小时、分钟、秒
        int year = calendar.get(Calendar.YEAR);      // 年
        int month = calendar.get(Calendar.MONTH) + 1; // 月（注意：月份从0开始，所以要加1）
        int day = calendar.get(Calendar.DAY_OF_MONTH); // 日
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 小时（24小时制）
        int minute = calendar.get(Calendar.MINUTE);   // 分钟
        int second = calendar.get(Calendar.SECOND);   // 秒

        // 拼接年月日时分秒，并转为字符串类型
        String timeStr = String.format("%04d%02d%02d%02d%02d%02d", year, month, day, hour, minute, second);
        System.out.println(timeStr);
        // 确保 orderid 是 String 类型，避免与数据库字段类型冲突
        order.setOrderid(timeStr); // 若数据库中的字段是 VARCHAR 类型

        order.setOrderstatus(0);
        order.setPaymenttime(null);
        System.out.println(order);
        // 插入订单
        orderDao.insert(order);
        return selectById(timeStr);
    }

    public Order selectById(String id) {
        return orderDao.selectByPrimaryKey(id);
    }

    public List<Order> selectAllOrder() {
        return orderDao.selectAllOrder();
    }

    public Order getOrderById(String id) {
        return orderDao.selectByPrimaryKey(id);
    }

    public int updateOrder(Order order) {
        return orderDao.updateByPrimaryKey(order);
    }

    public void update(Order order) {
        order.setOrderstatus(1);
        order.setPaymenttime(new Date());
        orderDao.updateByPrimaryKey(order);
    }

    public PageInfo<Order> searchOrder(Integer page, String nickname, Integer number) {
        PageHelper.startPage(page, 9);
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andOrderidLike("%" + nickname + "%");
        // 修改这里的条件判断逻辑，使其能获取0和1两种状态的数据
        if (number==-1 ) {
            criteria.andOrderstatusIn(Arrays.asList(0, 1));  // 假设andOrderStatusIn方法用于判断状态在给定列表中，可根据实际情况调整
        } else {
            criteria.andOrderstatusEqualTo(number);  // 其他情况还是按照原传入值精准匹配
        }

        List<Order> userList = orderDao.selectByExample(orderExample);
        PageInfo<Order> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Autowired
    private TicketDao ticketDao;
    @Autowired
    private ConcertDao concertDao;

    public List<Order> selectByUserId(Integer userid) {
        List<Order> orderList = orderDao.selectByUserId(userid);
        if (orderList.size() > 0) {
            orderList.stream().map(order -> {
                Concert concert = concertDao.selectByPrimaryKey(order.getConcertid());
                order.setConcert(concert);

                Seat seat = seatDao.selectByPrimaryKey(order.getSeatid());
                order.setSeat(seat);

                Ticket ticket = ticketDao.selectByPrimaryKey(order.getTicketid());
                order.setTicket(ticket);

                return order;
            }).collect(Collectors.toList());
        }
                // 可以在这里继续添加更多的逻辑，比如根据订单信息获取其他关联信息并设置到订单对象中
        return orderList;
    }
}
